package com.hck.utils;



import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

//sqlSessionFactory --> sqlSession  02
public class MybatisUtils {
    //https://mybatis.net.cn/getting-started.html    官网
    private  static SqlSessionFactory sqlSessionFactory;
    static {
        try {
            //使用Mybatis第一步 获取sqlSessionFactory对象
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //既然有了sqlSessionFactory，我们就可以从中获得sqlSession的实例
    //sqlSession完全包含了面向数据库执行SQL命令的所有方法
    public static SqlSession getsqlSession(){
                //获取sqlSession返回
        return sqlSessionFactory.openSession();
    }

}
